﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing;
using System.Text;
using JumbotPay.API.YunGouOS.WxPay;
namespace JumbotPay.WebFile.YungGouOS_Wxpay
{
    public partial class _default2 : JumbotPay.UI.BasicPage
    {
        public string Order_ID = "";
        public string Pay_Amount = "0.00";
        public string Product_Name = "";
        public string Product_Desc = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            NativePay nativePay = new NativePay();
            string appid = q("appid");//appid
            string order_id = q("order_id");//订单号
            Order_ID = order_id;//订单号
            string sign = q("sign");
            string key = new JumbotPay.DAL.AppDAL().GetKey(appid);
            if (key == "")
            {
                Response.Write("无效的appid");
                Response.End();
            }
            string[] array0 ={
			    "order_id="+order_id,
                 "appid="+appid
				};
            if (!JumbotPay.Utils.Strings.CheckSign(array0, key, sign))
            {
                Response.Write("验签失败");
                Response.End();
            }
            object[] _value = new JumbotPay.DAL.OrderDAL().GetOrderFields("wxpay", order_id, "", "product_name,product_desc,pay_amount,account_wxpay");
            Product_Name = _value[0].ToString();                            //商品名称
            Product_Desc = _value[1].ToString();                                 //商品描述
            Pay_Amount = _value[2].ToString();

            string account_wxpay = _value[3].ToString();

            WxPayData data = new WxPayData();
            data.SetValue("out_trade_no", new WxPayApi().GenerateOutTradeNo(account_wxpay), true);//随机字符串
            data.SetValue("total_fee", Pay_Amount, true);//总金额
            data.SetValue("body", Product_Name, true);//商品描述
            data.SetValue("type", "2");//直接生成二维码
            data.SetValue("attach", order_id);
            data.SetValue("return_url", site.Url + "/yungouos_wxpay/return_url.aspx?account_no=" + account_wxpay);


            string result = new WxPayApi().UnifiedOrder_Native(data, account_wxpay);//调用统一下单接口
            Newtonsoft.Json.Linq.JObject jo = Newtonsoft.Json.Linq.JObject.Parse(result);
            string _code = jo["code"].ToString();
            string url1 = "";
            if (_code == "0")
            {
                url1 = jo["data"].ToString();
            }
            else
            {
                JumbotPay.Utils.Logs.Error("wxpay_GetUnifiedOrder_Native", result, false);
            }
            Image1.ImageUrl = url1;
        }
    }
}